<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<jsp:include page="/WEB-INF/views/layouts/header.jsp"/>
<%
response.setHeader("Pragma","No-cache"); 
response.setHeader("Cache-Control","no-cache"); 
response.setDateHeader("Expires", 0); 

String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
// 网站地址
String netPath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort();
%>

<!-- kindeditor 编辑器 -->
<meta http-equiv="pragma" content="no-cache"> 
<meta http-equiv="cache-control" content="no-cache"> 
<meta http-equiv="expires" content="0"> 
<link rel="stylesheet" href="<%=basePath%>kindeditor/themes/default/default.css" />
<script charset="utf-8" src="<%=basePath%>kindeditor/kindeditor-min.js"></script>
<script charset="utf-8" src="<%=basePath%>kindeditor/lang/zh_CN.js"></script>

<script type="text/javascript">
// 错误信息
var sbReturnMessage = "${returnMessage}";
var isPic = "${newsTitle.ispic}";
var picurl = "${newsTitle.picurl}";
$(document).ready(function(){

	// 如果hasFile
	if(isPic==1){
		hideShowFile('1');
	}

	// 弹出错误提示窗口
	if(sbReturnMessage!=""){
		alert(sbReturnMessage);
	}

    $("[validate='true']").bind('blur', function() {
	  	validate(this);
	});
	
	$("[name=posttime]").bind('blur', function() {
	  	// 检查时间格式
		if(!checkPostTime()){
			alert("时间格式不正确。正确实例：2014-01-22 15:54:00！");
	    }
	});
});

var options={
	<%-- uploadJson: '<%=basePath%>article/ckEditorUploadImg',
	fileManagerJson : '<%=basePath%>article/ckEditorUploadFile',
	allowFileManager : true, // 是否允许上传本地图片
	allowFileManager: true,  // 是否允许浏览服务器已上传文件
	allowFlashUpload:false, // 是否允许上传Flash(默认)
	allowMediaUpload:false, // 是否允许上传多媒体文件(默认)
	//width: "100%", //编辑器的宽度为100%
	//height: "450px", //编辑器的高度为100px
	filterMode: false, //不会过滤HTML代码
	resizeMode: 1 //编辑器只能调整高度 --%>
	uploadJson: '<%=basePath%>article/ckEditorUploadImg',
	fileManagerJson : '<%=basePath%>article/ckEditorUploadFile',
	resizeType : 1,
	allowPreviewEmoticons : true,
	allowImageUpload : true,
	items : [
		'source', '|','fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold', 'italic', 'underline',
		'removeformat', '|', 'justifyleft', 'justifycenter', 'justifyright', 'insertorderedlist',
		'insertunorderedlist', 'insertfile', '|', 'emoticons', 'image', 'link']
};

KindEditor.ready(function(K) {
	//editor = K.create('textarea[name="newsContents.contents"]',options);
	editor = K.create('textarea[name="contents"]',options);
});

// 提交之前进行的验证
function doSubmit(){
	var errorMessage="";
	// 标题不能为空
	if($("#newsTitle").val()==null || 
		jQuery.trim($("#newsTitle").val())==""){
		errorMessage = errorMessage + "标题不能为空！";
	}

	// 内容不能为空
	var newsContent = editor.html();
	if(newsContent==null || jQuery.trim(newsContent)==""){
		if(errorMessage!=""){
			errorMessage = errorMessage + "\n\r" + "内容不能为空！";
		} else{
			errorMessage = errorMessage + "内容不能为空！";
		}
	}  else {
		$("[name='newsContents.contents']").val(newsContent);
	}

	// 这里栏目‘未定义’也应该允许提交
	setColumn();
	
	// 检查时间格式
	if(!checkPostTime()){
		if(errorMessage!=""){
			errorMessage = errorMessage + "\n\r" + "时间格式不正确。正确实例：2014-01-22 15:54:00！";
		} else{
			errorMessage = errorMessage + "时间格式不正确。正确实例：2014-01-22 15:54:00！";
		}
    }

    //如果选中有缩略图，则必须添加缩略图
    if($("#fileSpan").css("display")=="inline-block"){
    	if($("#fileSpan").val()==""&&picurl==""){
    		if(errorMessage!=""){
    			errorMessage = errorMessage + "\n\r" + "未添加缩略图！";
    		} else{
				errorMessage = errorMessage + "未添加缩略图！";
			}
    	}
    }
	
	if(errorMessage==""){
		//跳转
		$("#addSubmit").click();
	} else{
		alert(errorMessage);
	}
};

function validate(obj){
	var dataRequired = $(obj).attr("data-required");
	var dataType = $(obj).attr("data-type");
	var validateType = $(obj).attr("validate_type");
	if(dataRequired&&dataRequired==="true"){
		if(!$(obj).val()){
			var nullMessage = $(obj).attr("null-message");
			$(obj).next().text(nullMessage);
			$("[name=flag]").val("true");
		}else{
			$(obj).next().text("");
		}
	}
};

//显示与隐藏图片上传区域
function hideShowFile(v) {
	if (v == '1') {
		document.all.fileSpan.style.display = "";
	} else {
		document.all.fileSpan.style.display = "none";
	}
};

// 获得栏目id
function setColumn(){

	// 最后一个
	var seletedCol = $("select[name=selectedCol] option[selected]:last");
	var colID = seletedCol.val();
	if(colID==undefined){
		alert("获取栏目失败");
	} else {
		$("#cid").val(colID);
		$("#cname").val(seletedCol.text());
	}
};

// 检查时间格式
function checkPostTime(){
	var blnResult = true;
	var posttime = $("[name=posttime]").val();
	var reg = /^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))(\s(([01]\d{1})|(2[0123])):([0-5]\d):([0-5]\d))?$/;
	if (posttime!=""&&!reg.test(posttime)) {
		blnResult = false;
	}
	return blnResult;
}

</script>
<!--中间部分开始-->
<div class="main">
  <!-- 左侧 -->
  <jsp:include page="/WEB-INF/views/article/sidebar.jsp"/>
  <!-- /左侧 -->
  <!-- 核心内容区域 -->
<div id="iaas_layout_content" class="rightblock">
    <p class="shadow"></p>
    <div class="content" style="text-align:left;"><!--默认状态下引用样式ml60，展开后引用样式ml140-->
      <div class="mainblock">
        <div class="tabA">
          <h1>修改文章</h1>
        </div>
        <div class="zhgl_1 zhxxxg">
          <h5 style="font-size:16px; margin-top:15px;">标题信息</h5>
          <form:form id="form0" enctype="multipart/form-data" modelAttribute="newsTitle" 
            method="POST"  validate="true" action="/article/toUpdate/${newsTitle.id}">
          <table width="100%" border="0" cellpadding="0" cellspacing="0">
            <tr>
              <td width="8%"><p>标题：</p></td>
              <td width="92%">
              		<form:input id="newsTitle" path="title" validate="true" data-required="true" null-message=" 标题不能为空 ！"/>
                	<span class="cuered"></span>
               </td>
            </tr>
            <tr>
              <td><p>栏目：</p></td>
              <td>
               <!-- 1级必有，2级有显示，无则隐藏   3级如2级   readonly -->
              	<span class="columnChosen" >
              		<script type="text/javascript">
              			$.ajaxSetup ({ cache: false });
              			$(".columnChosen").load("<%=basePath%>column/loadColumn/" + ${newsTitle.cid});
              		</script>
                </span>
               </td>
            </tr>
            <tr>
              <td width="8%"><p>推荐：</p></td>
 			  <td width="92%">
              	<%-- <c:choose>
              		<c:when test="${ifelite!=null&&ifelite==1}">
              			<input type="checkbox" name="ifelite" value="1" checked="checked" style="width:20px;"/>
              		</c:when>
              		<c:otherwise>
              			<input type="checkbox" name="ifelite" value="0" style="width:20px;"/>
              		</c:otherwise>
              	</c:choose> --%>
              	<form:checkbox path="ifelite" value="1" cssStyle="width: 20px;"/>
               </td>
            </tr>

<%--         <tr>
              <td width="8%"><p>置顶：</p></td>
              <td width="92%" align="left">
              	<form:checkbox path="iftop" value="1"/>
               </td>
            </tr> --%>

            <%--  <tr>
              <td width="8%"><p>编辑者：</p></td>
              <td width="92%">
              	<form:input path="edituser"/>
               </td>
            </tr>
            
            <tr>
              <td width="8%"><p>作者：</p></td>
              <td width="92%">
              	<form:input path="author"/>
               </td>
            </tr> --%>
    
            <tr>
              <td width="8%"><p>发布时间：</p></td>
              <td width="92%">
				<form:input path="posttime" id="d242" onfocus="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss',startDate:'${strCdate}',alwaysUseStartDate:false})"
				 class="Wdate"  readonly="readonly"/>
               </td>
            </tr>
 
            <tr>
              <td width="8%"><p>来源 ：</p></td>
              <td width="92%">
              		<form:input path="copyfrom"/>
               </td>
            </tr>  
            <tr>
              <td width="8%"><p>点击 ：</p></td>
              <td width="92%">
              		<form:input path="pv"/>
               </td>
            </tr> 
          </table>
        </div> 
        
		<div class="zhgl_1 mmxg">
          <h5>内容信息</h5>
          <table width="100%" border="0" cellpadding="0" cellspacing="0">
            <tr>
              <td width="8%"><p>缩略图：</p></td>
              <td>
              	无：<form:radiobutton path="ispic" onclick="hideShowFile(this.value)" value="0" style="width:15px;height:22px;" ></form:radiobutton>
              	有：<form:radiobutton path="ispic" onclick="hideShowFile(this.value)" value="1" style="width:15px;height:22px;" id="hasFile"></form:radiobutton>
              	<input type="file" name="picFile" style="display: none" id="fileSpan"/>
              	<input type="hidden" name="picurl" value="${newsTitle.picurl}" />
              </td>
            </tr>
            <tr><td colspan="2" style="color:red;line-height: 0;padding-bottom: 20px;text-indent:38px;">请使用宽高比为760:480左右尺寸的图片，上传的图片名请勿包含中文及标点符号。</td></tr>
             <tr>
              <td width="8%"><p>内容摘要：</p></td>
              <td width="92%">
              	<form:textarea path="description" cssStyle="width:800px; height:100px;"/>  
              </td>
            </tr>
            
            <tr>
              <td width="8%"><p>新闻内容：</p></td>
              <td width="92%" style="padding-top: 10px;">
             	 <textarea name="contents" style="width:800px;height:400px;visibility:hidden;margin-top: 10px;">${newsTitle.newsContents.contents}</textarea>
             	 <input type="hidden" name="newsContents.contents" value='${newsTitle.newsContents.contents}'>
              </td>
            </tr>
          </table>
          <!-- 栏目ID -->
          <input type="hidden" name="cid" id="cid"/>
          <!-- 栏目名字 -->
          <input type="hidden" name="cname" id="cname"/>
          <input id="addSubmit" type="submit" style="display: none">
          </form:form>
          </div>
     	<div class="btnblock ">
          <input name="Input" type="button" value="保 存" class="btngreen01 linkhand" onclick="doSubmit()" style="cursor: pointer;"/>
        </div>
      </div>
    </div>
  </div>
</div> 
<!--中间部分结束-->
<!--底部开始-->
<jsp:include page="/WEB-INF/views/layouts/footer.jsp"/>
<!--底部结束-->

</body>
</html>
